package com.baichang.taoshunda.service.impl;


import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.DeliveryblancceDao;
import com.baichang.taoshunda.entity.DeliveryBalance;
import com.baichang.taoshunda.entity.DeliveryClerk;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/09/06.
 */
@Repository
public class DeliveryblancceDaoImpl extends BaseDaoImpl<DeliveryBalance> implements DeliveryblancceDao {
    @Override
    public List<Map<String, Object>> blanceList(Integer disId, String time, String endTime, Integer nowPage, int pageSize) {
        String sql = "SELECT db.id, db.delivery_id AS deliveryId, db.balance, db.created, db.order_number AS orderNumber ," +
                " db.balance_state AS balanceState, db.current_amount AS allMoney FROM t_delivery_balance db WHERE " +
                "db.delivery_id = ? AND TO_DAYS(db.created) >= TO_DAYS(?) AND TO_DAYS(db.created) <= TO_DAYS(?)  AND source_type!=2";
        return listForMapList(sql, nowPage, pageSize, disId, time, endTime);
    }

    @Override
    public Map<String, Object> getTodayMoney(Integer disId, String time, String endTime) {
        String sql = "SELECT IFNULL(SUM(db.balance),0) AS toDayMoney FROM t_delivery_balance db WHERE db.delivery_id = ? AND TO_DAYS(db.created) >= TO_DAYS(?) AND TO_DAYS(db.created) <= TO_DAYS(?)  AND source_type!=2";
        return queryForMap(sql, disId, time, endTime);
    }

    @Override
    public boolean updateMoney(DeliveryClerk deliveryClerk) {
        String sql = "UPDATE t_delivery_clerk SET balance=? WHERE id=?";
        return update(sql, deliveryClerk.getBalance(), deliveryClerk.getId()) > 0;
    }


}
